{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "a33a60be-1c4f-4515-b091-85c87e40be4b",
   "metadata": {},
   "source": [
    "# Robodyno Ultrasonic Sensor API"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "125f5f75-af84-44da-9a29-14a950856a90",
   "metadata": {},
   "source": [
    "## 1.初始化超声波传感器对象"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "d15fa1be-065e-43ff-ad96-f1474fdb902b",
   "metadata": {},
   "source": [
    "`UltrasonicSensor(can, id_)`\n",
    "\n",
    "参数：\n",
    "- `can` : can总线接口\n",
    "- `id_` : 超声波传感器模块ID，默认`0x35`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a878b6fb-9912-419e-bd75-c91c5e5e376b",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "from robodyno.components import UltrasonicSensor\n",
    "from robodyno.interfaces import CanBus\n",
    "can = CanBus()\n",
    "ultrasonic = UltrasonicSensor(can, 0x35)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "6220f4cb-76bc-49e1-a1cf-a54e9c6441c0",
   "metadata": {},
   "source": [
    "## 2.读取超声波传感器版本"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e0ce197b-dd36-463c-ac45-5133c86fe758",
   "metadata": {},
   "source": [
    "`get_version(timeout = 0)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- timeout : 请求超时时间(s)，0代表无超时时间\n",
    "\n",
    "返回值 ：\n",
    "\n",
    "- API版本dict\n",
    "    - `main_version` : 主版本号\n",
    "    - `sub_version` : 副版本号\n",
    "    - `type` : 设备类型\n",
    "        - ROBODYNO_ULTRASONIC_SENSOR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8c3f87a0-a132-432b-b22c-1a7dfdfbe5bc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'main_version': 0,\n",
       " 'sub_version': 1,\n",
       " 'type': <Model.ROBODYNO_ULTRASONIC_SENSOR: 164>}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ultrasonic.get_version(1)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "2c531d37-bfc6-4d0b-8194-c5fd3b4f2576",
   "metadata": {},
   "source": [
    "## 3.设置CAN_ID"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f8b595eb-f325-4d88-be72-28801d55d9ca",
   "metadata": {},
   "source": [
    "`config_can_bus(new_id = None, bitrate = 1000000)`\n",
    "\n",
    "参数 ：\n",
    "\n",
    "- `new_id` ：超声波传感器的新CAN_ID (默认：`0x35`)\n",
    "- `bitrate` : CAN总线通讯速率\n",
    "    - 1000000\n",
    "    - 500000\n",
    "    - 250000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2236fa59-4f9c-46a3-871c-8a2ebf6ad948",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "ultrasonic.config_can_bus(0x35)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "d9ed41f4-43aa-4bf3-a7a8-228f5e5c21d8",
   "metadata": {},
   "source": [
    "## 4.读取超声波的距离"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "8571ce8c-32e2-4ad5-8cf3-0f36790d6f2a",
   "metadata": {},
   "source": [
    "`get_distance(timeout=0.15)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- `timeout` : 请求超时时间(s)\n",
    "\n",
    "返回值:\n",
    "\n",
    "- 读取超声波传感器的距离值(cm)。\n",
    "  - 读取范围为`(20cm~250cm)`\n",
    "  - 如果读取超时或者超出范围，则为250cm。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "06c5b473-72ee-4c4b-99da-039c507489ab",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "33.98899841308594"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ultrasonic.get_distance()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d157423",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
